package com.ruoyi.help.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.help.domain.StudentAfter;
import com.ruoyi.help.mapper.StudentAfterMapper;
import com.ruoyi.help.service.IStudentAfterService;
import com.ruoyi.help.vo.StudentAfterVo;
import com.ruoyi.student.mapper.BblClassMapper;
import com.ruoyi.student.mapper.StudentMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 学生跟踪Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-20
 */
@Service
public class StudentAfterServiceImpl implements IStudentAfterService {

    @Autowired
    private StudentAfterMapper studentAfterMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private BblClassMapper bblClassMapper;


    /**
     * 查询学生跟踪
     *
     * @param id 学生跟踪主键
     * @return 学生跟踪
     */
    @Override
    public StudentAfter selectStudentAfterById(Long id) {
        return studentAfterMapper.selectStudentAfterById(id);
    }

    /**
     * 查询学生跟踪列表
     *
     * @param studentAfter 学生跟踪
     * @return 学生跟踪
     */
    @Override
    public List<StudentAfter> selectStudentAfterList(StudentAfter studentAfter) {
        return studentAfterMapper.selectStudentAfterList(studentAfter);
    }

    /**
     * 新增学生跟踪
     *
     * @param studentAfterVo 学生跟踪
     * @return 结果
     */
    @Override
    public int insertStudentAfter(StudentAfterVo studentAfterVo) {
        if (studentAfterVo.getStuName().size() == 0 || StringUtils.isEmpty(studentAfterVo.getClassName())) {
            return -5;
        }
        // 获取多选框中数据
        List<String> students = studentAfterVo.getStuName();
        // 拷贝相同字段到studentAfter对象
        StudentAfter studentAfter = new StudentAfter();
        BeanUtils.copyProperties(studentAfterVo, studentAfter);
        // 当前登录人
        studentAfter.setTeacherName(SecurityUtils.getUsername());
        // 获取所有跟踪 将stuName转为map key
        Map<String, StudentAfter> map = studentAfterMapper.findAll().stream()
                .collect(Collectors.toMap(StudentAfter::getStuName, ss -> ss));
        // 遍历stuName集合
        students.forEach(s -> {
            // 查询有没有跟踪过
            StudentAfter after = map.get(s);
            if (after != null) {
                BeanUtils.copyProperties(after, studentAfter);
                studentAfter.setAfterCount(studentAfter.getAfterCount() + 1);
                studentAfterMapper.updateStudentAfter(studentAfter);
            } else {
                studentAfter.setStuName(s);
                studentAfter.setAfterCount(1);
                studentAfterMapper.insertStudentAfter(studentAfter);
            }
        });
        return 1;
    }

    /**
     * 修改学生跟踪
     *
     * @param studentAfter 学生跟踪
     * @return 结果
     */
    @Override
    public int updateStudentAfter(StudentAfter studentAfter) {
        return studentAfterMapper.updateStudentAfter(studentAfter);
    }

    /**
     * 批量删除学生跟踪
     *
     * @param ids 需要删除的学生跟踪主键
     * @return 结果
     */
    @Override
    public int deleteStudentAfterByIds(Long[] ids) {
        return studentAfterMapper.deleteStudentAfterByIds(ids);
    }

    /**
     * 删除学生跟踪信息
     *
     * @param id 学生跟踪主键
     * @return 结果
     */
    @Override
    public int deleteStudentAfterById(Long id) {
        return studentAfterMapper.deleteStudentAfterById(id);
    }

}
